package com.scalar.chiptrack.tags;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

/**
 * User: Sujan
 */
public class attachFileTag extends TagSupport {
    private String fileGroup;
    private String fileSubGroup;
    private boolean allowMultiple = true;
    private boolean allowAttach = true;
    private boolean allowDelete = true;
    private String formTitle;
    private String linkText;
    private String styleClass;
    public final static String PARAM_FILE_SUB_GROUP_AUTO = "filesubgroupauto";

    public String getFileGroup() {
        return fileGroup;
    }

    public void setFileGroup(String fileGroup) {
        this.fileGroup = fileGroup;
    }

    public String getFileSubGroup() {
        return fileSubGroup;
    }

    public void setFileSubGroup(String fileSubGroup) {
        this.fileSubGroup = fileSubGroup;
    }

    public boolean isAllowMultiple() {
        return allowMultiple;
    }

    public void setAllowMultiple(boolean allowMultiple) {
        this.allowMultiple = allowMultiple;
    }

    public boolean isAllowAttach() {
        return allowAttach;
    }

    public void setAllowAttach(boolean allowAttach) {
        this.allowAttach = allowAttach;
    }

    public boolean isAllowDelete() {
        return allowDelete;
    }

    public void setAllowDelete(boolean allowDelete) {
        this.allowDelete = allowDelete;
    }


    public String getFormTitle() {
        return formTitle;
    }

    public void setFormTitle(String formTitle) {
        this.formTitle = formTitle;
    }

    public String getLinkText() {
        return linkText;
    }

    public void setLinkText(String linkText) {
        this.linkText = linkText;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            if("auto".equalsIgnoreCase(fileSubGroup)){
                fileSubGroup = pageContext.getRequest().getParameter(PARAM_FILE_SUB_GROUP_AUTO);
                if(fileSubGroup == null || fileSubGroup.trim().length()==0) {
                    fileSubGroup = "auto_"+System.currentTimeMillis();
                }
                out.println("<input type='hidden' name='"+PARAM_FILE_SUB_GROUP_AUTO+"' value='"+fileSubGroup+"'>\n");
            }
            out.println("<script>\n" +
                    "       function openAttachFile() {\n" +
                    "           var url = '" + ((HttpServletRequest)pageContext.getRequest()).getContextPath()+"/attachFile.do?action=showAttachFileForm';\n" +
                    "           url += '&fileGroup='+escape('"+fileGroup+"');\n" +
                    "           url += '&fileSubGroup='+escape('"+fileSubGroup+"');\n" +
                    "           url += '&allowMultiple='+escape('"+allowMultiple+"');\n" +
                    "           url += '&allowAttach='+escape('"+allowAttach+"');\n" +
                    "           url += '&allowDelete='+escape('"+allowDelete+"');\n" +
                    "           url += '&formTitle='+escape('"+formTitle+"');\n" +
                    "           url += '&linkText='+escape('"+linkText+"');\n" +
                    "           var newWindow=window.open(url,'attachFilePopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=yes,width=700,height=400,left=100,top=50,resizable=yes');\n" +
                    "           newWindow.moveTo(100,50);\n" +
                    "           newWindow.focus();\n" +
                    "       }\n" +
                    " </script>");

            out.println("<a href=\"javascript:openAttachFile()\""+ (styleClass!=null?"class='"+styleClass+"'" : "") +">"+linkText+"</a>");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return (EVAL_PAGE);
    }
}
